package com.tinshow.web.controller.topic;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tinshow.config.cookie.CookieUtil;
import com.tinshow.config.utils.CommonUtils;
import com.tinshow.config.utils.RelativeDateFormat;
import com.tinshow.config.utils.WebUtils;
import com.tinshow.model.article.Xinsheng;
import com.tinshow.model.article.vo.XinshengVo;
import com.tinshow.model.json.JsonResult;
import com.tinshow.model.user.User;
import com.tinshow.service.index.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by libo4 on 2017/6/3.
 */
@RestController
public class XinshengController {

    @Autowired
    ArticleService articleService;

    @RequestMapping(value = "submit_xinsheng")
    public Object submitXinsheng(@RequestParam(value = "xinsheng_detail") String xinshengDetail,
                                 HttpServletRequest request,HttpServletResponse response) throws Exception{
        Object o = request.getSession().getAttribute("user");
        JSONObject jsonObject = null;
        if (o != null) {
            jsonObject = (JSONObject) JSON.toJSON(o);
        }
        if(o == null){
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(0);
            jsonResult.setMsg("请登录后再来发表心声吧！");
            return jsonResult;
        }else{
            Xinsheng xinsheng = new Xinsheng();
            xinsheng.setUserId(Integer.parseInt(jsonObject.getString("userId")));
            xinsheng.setValue(xinshengDetail);
            articleService.insertXinsheng(xinsheng);
            List<XinshengVo> xinshengList = articleService.getXinshengByIndex();
            for (XinshengVo xinshengVo : xinshengList) {
                xinshengVo.setDateText(RelativeDateFormat.format(xinshengVo.getDtCreated()));
            }
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(1);
            jsonResult.setMsg("发表成功");
            jsonResult.setData(xinshengList);
            return jsonResult;
        }
    }

    @RequestMapping(value = "get_xinsheng", method = RequestMethod.GET)
    public Object getXinsheng(){
        List<XinshengVo> xinshengList = articleService.getXinshengByIndex();
        for (XinshengVo xinshengVo : xinshengList) {
            xinshengVo.setDateText(RelativeDateFormat.format(xinshengVo.getDtCreated()));
        }
        if(xinshengList.size() > 0){
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(1);
            jsonResult.setMsg("发表成功");
            jsonResult.setData(xinshengList);
            return jsonResult;
        }else{
            JsonResult jsonResult = new JsonResult();
            jsonResult.setSuccess(0);
            jsonResult.setMsg("");
            return jsonResult;
        }

    }
}
